package com.studentsystem.controller;

import cn.hutool.crypto.SecureUtil;
import com.studentsystem.common.Result;
import com.studentsystem.dataobject.StudentDO;
import com.studentsystem.dataobject.TeacherDO;
import com.studentsystem.dataobject.UserDO;
import com.studentsystem.model.Course;
import com.studentsystem.service.CourseService;
import com.studentsystem.service.StudentService;
import com.studentsystem.service.TeacherService;
import com.studentsystem.service.UserService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("admin")
public class AdminController {

    @Resource
    private UserService userService;

    @Resource
    private TeacherService teacherService;

    @Resource
    private StudentService studentService;

    @Resource
    private CourseService courseService;

    @RequiresRoles("admin")
    @GetMapping("authorization")
    public Result<Object> authorization() {
        return Result.success(null);
    }


    @GetMapping("getUsersByRole")
    public Result<List<UserDO>> getUsersByRole(@RequestParam("role") String role) {
        return Result.success(userService.getUsersByRole(role));
    }

    @PutMapping("updateUser")
    public Result<String> update(@RequestBody UserDO userDO) {
        userDO.setPwd(SecureUtil.md5(userDO.getPwd()));
        userDO.setModifiedTime(LocalDateTime.now());
        Boolean update = userService.update(userDO);
        if (update) {
            return Result.success("修改成功");
        } else {
            return Result.fail("修改失败", null);
        }
    }

    @GetMapping("searchUser")
    public Result<List<UserDO>> search(@RequestParam("param") String param,
                                       @RequestParam("role") String role) {
        if (!StringUtils.isEmpty(param)) {
            return Result.success(userService.getUsersByParamAndRole(param, role));
        }
        return Result.success(new ArrayList<>());
    }

    @GetMapping("getTeachers")
    public Result<List<TeacherDO>> getTeachers() {
        return Result.success(teacherService.queryAll());
    }

    @GetMapping("searchTeacher")
    public Result<List<TeacherDO>> searchTeacher(@RequestParam("param") String param) {
        if (!StringUtils.isEmpty(param)) {
            return Result.success(teacherService.getTeachersByParam(param));
        } else {
            return Result.success(new ArrayList<>());
        }
    }

    @PostMapping("addOrUpdateTeacher")
    public Result<String> addOrUpdateTeacher(@RequestBody TeacherDO teacherDO) {
        teacherDO.setModifiedTime(LocalDateTime.now());
        Boolean result;
        if (teacherDO.getId() == null) {
            teacherDO.setCreatedTime(LocalDateTime.now());
            result = teacherService.insert(teacherDO);
        } else {
            result = teacherService.update(teacherDO);
        }

        if (result) {
            return Result.success("操作成功");
        } else {
            return Result.fail("操作失败", null);
        }
    }

    @DeleteMapping("deleteTeacher")
    public Result<String> deleteTeacher(@RequestParam("teacherId") Long teacherId) {
        if (StringUtils.isEmpty(teacherId)) {
            return Result.fail("教师ID为空", null);
        }
        boolean delete = teacherService.deleteById(teacherId);
        if (delete) {
            return Result.success("删除成功");
        } else {
            return Result.fail("删除失败", null);
        }
    }

    @GetMapping("getStudents")
    public Result<List<StudentDO>> getStudents() {
        return Result.success(studentService.queryAll());
    }

    @GetMapping("searchStudent")
    public Result<List<StudentDO>> searchStudent(@RequestParam("param") String param) {
        if (!StringUtils.isEmpty(param)) {
            return Result.success(studentService.getStudentsByParam(param));
        } else {
            return Result.success(new ArrayList<>());
        }
    }

    @PostMapping("addOrUpdateStudent")
    public Result<String> addOrUpdateStudent(@RequestBody StudentDO studentDO) {
        studentDO.setModifiedTime(LocalDateTime.now());
        Boolean result;
        if (studentDO.getId() == null) {
            studentDO.setCreatedTime(LocalDateTime.now());
            result = studentService.insert(studentDO);
        } else {
            result = studentService.update(studentDO);
        }

        if (result) {
            return Result.success("操作成功");
        } else {
            return Result.fail("操作失败", null);
        }
    }

    @DeleteMapping("deleteStudent")
    public Result<String> deleteStudent(@RequestParam("studentId") Long studentId) {
        if (StringUtils.isEmpty(studentId)) {
            return Result.fail("学生ID为空", null);
        }
        boolean delete = studentService.deleteById(studentId);
        if (delete) {
            return Result.success("删除成功");
        } else {
            return Result.fail("删除失败", null);
        }
    }

    @GetMapping("getCourses")
    public Result<List<Course>> getCourses() {
        return Result.success(courseService.queryAll());
    }

    @GetMapping("searchCourse")
    public Result<List<Course>> searchCourse(@RequestParam("param") String param) {
        if (!StringUtils.isEmpty(param)) {
            return Result.success(courseService.getCoursesByParam(param));
        } else {
            return Result.success(new ArrayList<>());
        }
    }

    @PostMapping("addOrUpdateCourse")
    public Result<String> addOrUpdateCourse(@RequestBody Course course) {
        course.setModifiedTime(LocalDateTime.now());
        Boolean result;
        if (course.getId() == null) {
            course.setCreatedTime(LocalDateTime.now());
            result = courseService.insert(course);
        } else {
            result = courseService.update(course);
        }

        if (result) {
            return Result.success("操作成功");
        } else {
            return Result.fail("操作失败", null);
        }
    }

    @DeleteMapping("deleteCourse")
    public Result<String> deleteCourse(@RequestParam("courseId") Long courseId) {
        if (StringUtils.isEmpty(courseId)) {
            return Result.fail("课程ID为空", null);
        }
        boolean delete = courseService.deleteById(courseId);
        if (delete) {
            return Result.success("删除成功");
        } else {
            return Result.fail("删除失败", null);
        }
    }
}
